package com.tcoiss.webservice.utils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import com.tcoiss.common.core.enums.ErroEnums;
import com.tcoiss.common.core.exception.CustomException;
import org.apache.axis.utils.XMLUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;


public class DomUtils {

    public static Document newDocument(String xmlStr, boolean nsaware) {
        return newDocument(xmlStr, nsaware, "UTF-8");
    }

    public static Document newDocument(String xmlStr, boolean nsaware,
                                       String encoding) {
        ByteArrayInputStream stream = null;
        try {
            DocumentBuilderFactory xmlFact = DocumentBuilderFactory
                    .newInstance();
            xmlFact.setNamespaceAware(nsaware);
            DocumentBuilder builder = xmlFact.newDocumentBuilder();
            stream = new ByteArrayInputStream(xmlStr.getBytes(encoding));

            return builder.parse(stream);
        } catch (Exception e) {
            //throw new ApiException("-1", null,"从字符串创建DOM对象失败"+e.getMessage());
            throw new CustomException(ErroEnums.SYSERRO.getCode(),new Object[]{},
                    ErroEnums.SYSERRO.getInfo() + "从字符串创建DOM对象失败"+e.getMessage());
        } finally {
            try {
                if (stream != null)
                    stream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public static String ElementToString(Element element) {
        return XMLUtils.ElementToString(element);
    }

    public static String DocumentToString(Document doc) {
        return XMLUtils.DocumentToString(doc);
    }

    /**
     *
     * 将传入的一个XML String转换成一个org.w3c.dom.Document对象返回。
     *
     *
     *
     * @param xmlString
     *
     *            一个符合XML规范的字符串表达。
     *
     * @return a Document
     */

    public static Document parseXMLDocument(String xmlString) {

        if (xmlString == null) {

            throw new IllegalArgumentException();

        }

        try {

            return newDocumentBuilder().parse(

                    new InputSource(new StringReader(xmlString)));

        } catch (Exception e) {

            throw new RuntimeException(e.getMessage());

        }

    }

    /**
     *
     * 初始化一个DocumentBuilder
     *
     *
     *
     * @return a DocumentBuilder
     *
     * @throws
     */

    public static DocumentBuilder newDocumentBuilder()

            throws Exception {

        return newDocumentBuilderFactory().newDocumentBuilder();

    }

    /**
     *
     * 初始化一个DocumentBuilderFactory
     *
     *
     *
     * @return a DocumentBuilderFactory
     */

    public static DocumentBuilderFactory newDocumentBuilderFactory() {

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setExpandEntityReferences(false);
        dbf.setNamespaceAware(true);

        return dbf;

    }
}
